package com.morehair.service.wx.impl;

import com.morehair.bean.pojo.*;
import com.morehair.bean.vo.wx.GoodsOfFloorVo;
import com.morehair.bean.vo.wx.GoodsOfGrouponVo;
import com.morehair.bean.vo.wx.HomeIndexVo;
import com.morehair.mapper.*;
import com.morehair.service.wx.WxHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Date: 2021/6/8
 * Time: 21:46
 * author: zhang wei
 */

@Service
@Transactional
public class WxHomeServiceImpl implements WxHomeService {

    @Autowired
    AdMapper adMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    GrouponRulesMapper grouponRulesMapper;


    @Override
    public HomeIndexVo getHomeIndex() {

        List<HomeIndexVo.NewGoodsList> newGoodsLists = new ArrayList<>();
        List<Goods> goods = goodsMapper.selectByRandom();
        for (Goods good : goods) {
            HomeIndexVo.NewGoodsList newGoodsList = new HomeIndexVo.NewGoodsList();
            newGoodsList.setId(good.getId());
            newGoodsList.setName(good.getName());
            newGoodsList.setBrief(good.getBrief());
            newGoodsList.setPicUrl(good.getPicUrl());
            newGoodsList.setIsNew(good.getIsNew());
            newGoodsList.setIsHot(good.getIsHot());
            newGoodsList.setCounterPrice(good.getCounterPrice());
            newGoodsList.setRetailPrice(good.getRetailPrice());
            newGoodsLists.add(newGoodsList);
        }


        List<HomeIndexVo.CouponList> couponLists = new ArrayList<>();
        List<Coupon> coupons = couponMapper.selectByRandom();
        for (Coupon coupon : coupons) {
            HomeIndexVo.CouponList couponList = new HomeIndexVo.CouponList();
            couponList.setId(coupon.getId());
            couponList.setName(coupon.getName());
            couponList.setDays(coupon.getDays());
            couponList.setDesc(coupon.getDesc());
            couponList.setDiscount(coupon.getDiscount());
            couponList.setTag(coupon.getTag());
            couponList.setMin(coupon.getMin());
            couponLists.add(couponList);
        }

        List<HomeIndexVo.Channel> channels = new ArrayList<>();
        List<Category> categories = categoryMapper.selectByRandom();
        System.out.println("***********************************");
        System.out.println(categories);
        System.out.println("***********************************");
        for (Category category : categories) {
            HomeIndexVo.Channel channel = new HomeIndexVo.Channel();
            channel.setId(category.getId());
            channel.setName(category.getName());
            channel.setIconUrl(category.getIconUrl());
            channels.add(channel);
        }

        List<HomeIndexVo.GrouponList> grouponLists = new ArrayList<>();
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByRandom();
        for (GrouponRules grouponRule : grouponRules) {
            HomeIndexVo.GrouponList grouponList = new HomeIndexVo.GrouponList();

            grouponList.setGroupon_member(grouponRule.getDiscountMember());

            //计算优惠后价格
            //优惠价格
            BigDecimal discount = grouponRule.getDiscount();//折扣
            //去goods里获取原价
            Integer goodsId = grouponRule.getGoodsId();
            Goods primaryKey = goodsMapper.selectByPrimaryKey(goodsId);
            //原价
            BigDecimal retailPrice = primaryKey.getRetailPrice();
            //最终价格
            BigDecimal finalPrice = retailPrice.subtract(discount);

            //BigDecimal转换成double类型
            double v = finalPrice.doubleValue();
            grouponList.setGroupon_price(v);

            GoodsOfGrouponVo goodsOfGrouponVo = new GoodsOfGrouponVo();
            goodsOfGrouponVo.setId(primaryKey.getId());
            goodsOfGrouponVo.setName(primaryKey.getName());
            goodsOfGrouponVo.setBrief(primaryKey.getBrief());
            goodsOfGrouponVo.setPicUrl(primaryKey.getPicUrl());
            goodsOfGrouponVo.setCounterPrice(primaryKey.getCounterPrice());
            goodsOfGrouponVo.setRetailPrice(primaryKey.getRetailPrice());

            grouponList.setGoods(goodsOfGrouponVo);

            grouponLists.add(grouponList);
        }


        List<HomeIndexVo.Banner> bannerList = adMapper.selectToBanner();


        List<HomeIndexVo.BrandList> brandListList = brandMapper.selectToBrandList();


        List<HomeIndexVo.HotGoodsList> hotGoodsListList = goodsMapper.selectToHotGoodsList();


        List<HomeIndexVo.TopicList> topicListList = topicMapper.selectToTopicList();


        //四个类，每个类4个商品，四个商品为同一个大类下的同一个小类，最外面只显示大类名字
        List<HomeIndexVo.FloorGoodsList> floorGoodsListList = categoryMapper.selectToFloorGoods();
        for (HomeIndexVo.FloorGoodsList floorGoodsList : floorGoodsListList) {
            Integer firstLevelCategoryId = floorGoodsList.getId();//一级目录Id
            Category categoryForSid = categoryMapper.selectByPid(firstLevelCategoryId);//二级目录Id

            //通过二级Id去随机拿四个商品
            List<GoodsOfFloorVo> goodsOfFloorVos = goodsMapper.selectBySid(categoryForSid);

            floorGoodsList.setGoodsList(goodsOfFloorVos);
        }


        /**
         * 进阶版本
         */
        // boolean[] isIt = new boolean[4];
        // for (int i = 0; i < isIt.length; i++) {
        //     isIt[i] = false;
        // }
        // HomeIndexVo.FloorGoodsList[] array = new HomeIndexVo.FloorGoodsList[4];
        // List<HomeIndexVo.FloorGoodsList> floorGoodsListList;
        // do {
        //     floorGoodsListList = categoryMapper.selectToFloorGoods();
        //     HomeIndexVo.FloorGoodsList[] floorGoodsLists = floorGoodsListList.toArray(array);
        //     for (int i = 0; i < floorGoodsLists.length; i++) {
        //         Integer firstLevelCategoryId = floorGoodsLists[i].getId();//一级目录Id
        //         List<GoodsOfFloorVo> goodsOfFloorVos;
        //         int cont = 0;
        //         boolean tag = false;
        //         do {
        //             Category categoryForSid = categoryMapper.selectByPid(firstLevelCategoryId);//二级目录Id
        //             //通过二级Id去随机拿四个商品
        //             goodsOfFloorVos = goodsMapper.selectBySid(categoryForSid);
        //             cont++;
        //             if (cont > 15) {
        //                 tag = true;
        //                 break;
        //             }
        //         } while (goodsOfFloorVos.size() < 3);
        //         if(tag == false){
        //             //商品够
        //             floorGoodsLists[i].setGoodsList(goodsOfFloorVos);
        //             isIt[i] = true;
        //         }else{
        //             //不够,
        //             break;
        //         }
        //     }
        // } while (isIt[0] == false||isIt[1] == false||isIt[3] == false||isIt[4] == false);
        // // for (int i = 0; i < floorGoodsLists.length; i++) {
        // //     floorGoodsListList.add(floorGoodsLists[i]);
        // // }

        HomeIndexVo homeIndexVo = new HomeIndexVo(
                newGoodsLists, couponLists,
                channels, grouponLists,
                bannerList,
                brandListList,
                hotGoodsListList, topicListList,
                floorGoodsListList);
        return homeIndexVo;
    }


}
